#ifndef INPUTHANDLER_H_INCLUDED
#define INPUTHANDLER_H_INCLUDED


#include <utility>
#include <vector> 
#include <array>
#include <SDL.h> 
#include "Vector2D.h"


// MVSE2010 doesn't support enum class :(
enum MouseButtons {LEFT, MIDDLE, RIGHT}; 


class InputHandler
{
public: 
    static InputHandler* Instance()
	{
		if (s_pInstance == nullptr)
		{
			s_pInstance = new InputHandler(); 
		}

		return s_pInstance; 
	}

	// Initialize all available joysticks
	void initJoy(); 

	// Initialize the mouse
	void initMouse(); 

	// Are all available joysticks initialized?
	bool areJoyInit() const { return m_bJoyInit; } 
	
	void Update(); 
	void CleanUp(); 

	float xAxis(int, int) const; 
	float yAxis(int, int) const;

	// Check for a button press
	bool joyButtonState(int, int) const; 
	bool mouseButtonState(int) const;
	// Is any key pressed or not?
	bool isKeyDown(SDL_Scancode); 

	Vector2D m_mousePosition; 

private: 
	InputHandler() {}; 
	~InputHandler() {}; 

	// Handle keyboard events 
	void onKeyDown(); 
	void onKeyUp(); 

	// Handle mouse events
	void onMouseMove(SDL_Event&);
	void onMouseButtonDown(SDL_Event&);
	void onMouseButtonUp(SDL_Event&);

	// Handle joysticks events	
	void onJoystickAxisMove(SDL_Event&);
	void onJoystickButtonDown(SDL_Event&);
	void onJoystickButtonUp(SDL_Event&);

	std::vector <SDL_Joystick*> m_joysticks; 
	// For multiple joysticks with their respective axes
	std::vector <std::pair <Vector2D*, Vector2D*> > m_joystickValues; 
	// Avoid vector <bool> (the original version)!
	std::vector <std::vector <unsigned char> > m_joyButtonStates;  
	
	std::array <bool, 3> m_mouseButtonStates; 
	// Are all joysticks connected?
	bool m_bJoyInit; 
	// Pointer to SDL key array
	static const Uint8* m_keystate; 

	static InputHandler* s_pInstance;
}; 


#endif // INPUTHANDLER_H_INCLUDED